package com.example.pkmedical.Utils;

import static android.text.TextUtils.isEmpty;

public class NumberUtil {
    /**
     * desc:将数组转为16进制
     * @param bArray
     * @return
     * modified:
     */
    public static String bytesToHexString(byte[] bArray) {
        if(bArray == null){
            return null;
        }
        if(bArray.length == 0){
            return "";
        }
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    /*String转byte数组*/
    public static byte[] Stringtobytes(String s) {
        byte[] present = {};
        if (Integer.parseInt(s) >= 16) {
            present = hexString2Bytes(Integer.toHexString(Integer.parseInt(s)));
        }else if(Integer.parseInt(s) == 0){
            present = new byte[]{0x00};
        }else if(Integer.parseInt(s) == 1){
            present = new byte[]{0x01};
        }else if(Integer.parseInt(s) == 2){
            present = new byte[]{0x02};
        }else if(Integer.parseInt(s) == 3){
            present = new byte[]{0x03};
        }else if(Integer.parseInt(s) == 4){
            present = new byte[]{0x04};
        }else if(Integer.parseInt(s) == 5){
            present = new byte[]{0x05};
        }else if(Integer.parseInt(s) == 6){
            present = new byte[]{0x06};
        }else if(Integer.parseInt(s) == 7){
            present = new byte[]{0x07};
        }else if(Integer.parseInt(s) == 8){
            present = new byte[]{0x08};
        }else if(Integer.parseInt(s) == 9){
            present = new byte[]{0x09};
        }else if(Integer.parseInt(s) == 10){
            present = new byte[]{0x0a};
        }else if(Integer.parseInt(s) == 11){
            present = new byte[]{0x0b};
        }else if(Integer.parseInt(s) == 12){
            present = new byte[]{0x0c};
        }else if(Integer.parseInt(s) == 13){
            present = new byte[]{0x0d};
        }else if(Integer.parseInt(s) == 14){
            present = new byte[]{0x0e};
        }else if(Integer.parseInt(s) == 15){
            present = new byte[]{0x0f};
        }

        return present;
    }
    /*
     * 16进制字符串转字节数组
     */
    public static byte[] hexString2Bytes(String hex) {

        if ((hex == null) || (hex.equals(""))){
            return null;
        }
        else if (hex.length()%2 != 0){
            return null;
        }
        else{
            hex = hex.toUpperCase();
            int len = hex.length()/2;
            byte[] b = new byte[len];
            char[] hc = hex.toCharArray();
            for (int i=0; i<len; i++){
                int p=2*i;
                b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p+1]));
            }
            return b;
        }

    }
    /*
     * 字符转换为字节
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
    //把整数字符串，转变成byte字节
    public static byte hexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }
    /**
     * desc:将16进制的数据转为数组
     * <p>创建人：聂旭阳 , 2014-5-25 上午11:08:33</p>
     * @param data
     * @return
     * modified:
     */
    public static byte[] stringToBytes(String data){
        String hexString=data.toUpperCase().trim();
        if (hexString.length()%2!=0) {
            return null;
        }
        byte[] retData=new byte[hexString.length()/2];
        for(int i=0;i<hexString.length();i++)
        {
            int int_ch;  // 两位16进制数转化后的10进制数
            char hex_char1 = hexString.charAt(i); ////两位16进制数中的第一位(高位*16)
            int int_ch3;
            if(hex_char1 >= '0' && hex_char1 <='9')
                int_ch3 = (hex_char1-48)*16;   //// 0 的Ascll - 48
            else if(hex_char1 >= 'A' && hex_char1 <='F')
                int_ch3 = (hex_char1-55)*16; //// A 的Ascll - 65
            else
                return null;
            i++;
            char hex_char2 = hexString.charAt(i); ///两位16进制数中的第二位(低位)
            int int_ch4;
            if(hex_char2 >= '0' && hex_char2 <='9')
                int_ch4 = (hex_char2-48); //// 0 的Ascll - 48
            else if(hex_char2 >= 'A' && hex_char2 <='F')
                int_ch4 = hex_char2-55; //// A 的Ascll - 65
            else
                return null;
            int_ch = int_ch3+int_ch4;
            retData[i/2]=(byte) int_ch;//将转化后的数放入Byte里
        }
        return retData;
    }
    /**
     * Description: 负数byte转正int <BR>
     *
     * @param b
     * @return
     * @author dsn
     * @date 2018年12月29日 下午3:29:38
     * @version 1.0
     */
    public static Integer byteToInteger(Byte b) {
        return 0xff & b;
    }

    /**
     * 检查字符是否为 null || ""
     * 如果是 null 或者 "" 则返回false ,反则为 true
     * @param value 需要验证的多个字符，以英文逗号分隔
     * @return
     */
    public static boolean checkStringIsNull(String... value){
        int count = 0;
        for(int i=0;i<value.length;i++){
            //遍历字符数组所有的参数，发现某个为 null 或者 "" ,则跳出
            if(isEmpty(value[i])){
                return false;
            }
            count++;
        }
        if(count == value.length){
            return true;
        }
        return false;
    }
}
